package com.cdream.basic.questiion;

import cn.hutool.core.collection.CollUtil;
import lombok.Data;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;

/**
 * @author cdream
 * @date 2022/2/27
 */
public class RandomQuestion {
    /**
     * 多线程题目
     */
    private static List<Question> THREAD;
    /**
     * 微服务题目
     */
    private static List<Question> MICRO_SERVICE;


    public static void main(String[] args) {
        THREAD = loadQuestion("多线程");
        MICRO_SERVICE = loadQuestion("微服务");
        randomQuestion();
    }

    public static List<Question> loadQuestion(String path) {
        List<Question> questionList = CollUtil.newArrayList();
        try (BufferedReader br = new BufferedReader(new FileReader(new File("doc/question/" + path + ".txt")))) {
            String line;
            while ((line = br.readLine()) != null) {
                questionList.add(new Question(line));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return questionList;
    }

    public static void randomQuestion() {

        System.out.println(THREAD.get(getRandomIndex(THREAD)).content);
        System.out.println(MICRO_SERVICE.get(getRandomIndex(MICRO_SERVICE)).content);

    }

    private static int getRandomIndex(List<Question> questions) {
        return (int) System.currentTimeMillis() % questions.size();
    }

    @Data
    static class Question {
        private String content;
        private String answer;
        private String answerUrl;

        public Question(String content, String answer) {
            this.content = content;
            this.answer = answer;
        }

        public Question(String content) {
            this.content = content;
        }

    }
}
